//
//  WeatherListViewController.swift
//  MyWeatherApp
//
//  Created by Shengbing Li (QXU7859) on 2021/2/27.
//  Copyright © 2021 shanghai. All rights reserved.
//

import UIKit
import ObjectMapper

class WeatherListViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var woeid: Int?
    private var weatherModel: WeatherModel?
    private lazy var weatherHeaderView: WeatherHeaderView = {
        let headerView = WeatherHeaderView.instantiateFromNib()
        headerView.backgroundColor = UIColor.lightGray
        headerView.bounds.size.height = 300
        return headerView
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        title = "天气详情"
        updateUI()
        loadData()
    }
    
    private func updateUI() {
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellIndentifier")
        tableView.tableFooterView = UIView()
        tableView.tableHeaderView = weatherHeaderView
    }
    
    private func loadData() {
        HudView.share.beginHud(T: view)
        self.tableView.isHidden = true
        let weatherRequest = WeatherRequest(woeid: "\(self.woeid ?? 44418)")
        API.current()?.fetch(weatherRequest, complete: { (response) in
            HudView.share.HideHud()
            self.tableView.isHidden = false
            guard let json = response.raw?.dictionaryObject else { return }
            self.weatherModel = Mapper<WeatherModel>().map(JSON: json)

            self.tableView.reloadData()
            self.setUpHeaderViewData()
        })
    }
    
    private func setUpHeaderViewData() {
        let cityName = self.weatherModel?.title ?? ""
        let time = self.weatherModel?.time ?? ""
        self.weatherHeaderView.setConfiguration(cityName: cityName, rain: time, rainImage: UIImage())
    }
}

extension WeatherListViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return self.weatherModel?.consolidated_weather?.count ?? 0
        case 1:
            return self.weatherModel?.sources?.count ?? 0
        default:
            return 0
        }
        
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellIndentifier", for: indexPath)
        switch indexPath.section {
        case 0:
            cell.textLabel?.text = self.weatherModel?.consolidated_weather?[indexPath.row].weather_state_name ?? ""
        case 1:
            cell.textLabel?.text = self.weatherModel?.sources?[indexPath.row].title ?? ""
        default:
            break
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if section == 0 {
            return "consolidated_weather data"
        }else if section == 1 {
            return "sources data"
        }
        return ""
    }
}
